<?php

namespace app\job;

use app\api\service\WechatSubscribeService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\queue\Job;

/**
 * 微信订阅消息推送任务
 */
class WechatSubscribeSendJob
{


    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function fire(Job $job, $data)
    {
        flog([
            'message' => '微信订阅消息发送开始',
            'data' => $data,
        ],'wechat_subscribe_send');
        $rt = $this->doJob($data);
        if($rt){
            $job->delete();
            return true;
        }

        // 重试三次失败
        if($job->attempts() == 3){
            $job->delete();
            return false;
        }

        //执行失败10S后重试
        $job->release(10);


    }

    /**
     * 发送任务
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function doJob($data): bool
    {
        return WechatSubscribeService::doSend($data);
    }


    public function failed($data)
    {

    }
}